package com.my.controller.System;


import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.my.bean.ResultPage;
import com.my.bean.SysDic;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.RoleUserService;
import com.my.service.UserService;
import com.my.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-25
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RoleUserService roleUserService;
    //跳转 登陆页面
    @RequestMapping("/main")
    public String main(User user, HttpSession session){
       /* return "index";*/
        return "login";
    }
    //跳转 首页
    @RequestMapping("/index")
    public String index(){

      return "index";
    }

    //跳转编辑用户
    @RequestMapping("/updateUser.html")
    public String ToupdateUser(){

        return "/Alert/user_edit";
    }
    //跳转 首页
    @RequestMapping("/findMenu")
    @ResponseBody
    public User findMenu(HttpSession session){
        User user=new User();
        user=(User)session.getAttribute("user");
      return user;
    }
    //子页面请求
    @RequestMapping("/home")
    public String home(){
        return "home";

    }
    //登陆方法
    @RequestMapping("/login")
    @ResponseBody
    public String index(User user, HttpSession session){
     return  userService.login(user,session);
    }


    //跳转系统管理的人员管理页面

    @RequestMapping("/userAdmin")
    public String userAdmin(User user, HttpSession session){
        return  "/System/user";
    }
    //系统管理 查询所有用户信息 包括用户的角色
    @RequestMapping("/userAdminList")
    @ResponseBody
    public ResultPage<User> userAdminList(VoPage voPage,User user,HttpSession session){

        boolean admin=false;
//如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        ResultPage<User> resultPage= userService.findUserAndRole(voPage,user,areaid,admin);
        return resultPage;
    }
    //新增用户
    @RequestMapping("/insertUser")
    @ResponseBody
    public String insertUser(String param){
        userService.insertUser(param);
        return "添加成功";
    }

    //编辑用户
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(String param){
        User user=JSONObject.parseObject(param,User.class);
        if(userService.updateById(user)){
            return  "修改成功";
        }
        return  "失败";

    }




    //退出系统
    @RequestMapping("/exit")
    @ResponseBody
    public String exit(HttpSession session){
        session.removeAttribute("user");
        return "true";
    }


    //房态管理 查询所有用户
    @RequestMapping("/findUserAll")
    @ResponseBody
    public List<User> findUserAll( ){
        Map map=new HashMap<>();

        return  userService.selectByMap(map);
    }





    //登陆方法
    @RequestMapping("/test")
    @ResponseBody
    public String test(  String a){
    List<User> users= JSONArray.parseArray(a,User.class);

        return "true";
    }
    //
    @RequestMapping("/info")
    public String info(){
        return "/index/test";
    }
    @RequestMapping("/info2")
    public String info2(){
        return "/index/test2";
    }
    //表格
    @RequestMapping("/list")
    @ResponseBody
    public ResultPage<User> list(VoPage voPage,User user){
      ResultPage<User> resultPage= userService.selectMyPage(voPage,user);
        return resultPage;
    }



    @RequestMapping("/user_add")
    public String user_add (){
        return "/Alert/user_add";
    }
    @RequestMapping("/user_edit")
    public String user_edit(){
        return "/Alert/user_edit";
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(Long id){
        try {
           return userService.deleteUser(id);
        } catch (SQLException e) {
            return  "删除失败";
        }
    }






}
